﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PrinteMaster
{
    public partial class 充值窗口 : Form
    {

        private string m_lpstrId="",m_lpstrIdHit,m_lpstrMoneyHit;
        private int m_intMoney=0;
        public 充值窗口()
        {
            InitializeComponent();
        }

        private void 充值窗口_Load(object sender, EventArgs e)
        {
            m_lpstrIdHit = textBox充值账号.Text;
            m_lpstrMoneyHit = textBox充值金额.Text;
        }
        /// <summary>
        /// 静态创建充值窗口
        /// </summary>
        /// <param name="lpstrName"></param>
        /// <returns></returns>
        public static 充值窗口 create(string lpstrName,IWin32Window window)
        {
            充值窗口 c = new 充值窗口();
            c.Show(window);
            return c;
        }

        private void textBox充值金额_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsNumber(e.KeyChar) && e.KeyChar != (char)8)
            {
                e.Handled = true;
            }
            
        }
        /// <summary>
        /// 流水单号
        /// </summary>
        private string m_lpstrTradeNumber;
        /// <summary>
        /// 用户编号
        /// </summary>
        private string m_lpstrUserID;
        /// <summary>
        /// 用户姓名
        /// </summary>
        private string m_lpstrUserName;
        /// <summary>
        /// 用户名
        /// </summary>
        private string m_lpstrUser;
        private void button充值_Click(object sender, EventArgs e)
        {
            if (!textBox充值账号.Text.Equals("") && !textBox充值账号.Text.Equals(m_lpstrIdHit))
                m_lpstrId = textBox充值账号.Text;

            // 判断充值金额输入框是否处于已输入状态;
            if (!textBox充值金额.Text.Equals("") && !textBox充值金额.Text.Equals(m_lpstrMoneyHit))
                m_intMoney = Convert.ToInt32(textBox充值金额.Text);

            // 检查参数
            if (m_lpstrId.Equals(""))
            {
                // 
                MessageBox.Show("请输入账号");
                return;
            }
            if (m_intMoney == 0)
            {
                //
                MessageBox.Show("请输入充值金额");
                return;
            }

            // 如果账号和金额都正确的被输入,提示确认充值
            ////////////////// 检测参数是否准确 //////////////
            MySqlHelper.UserInfo info;
            if (!MySqlHelper.userInfo("会员简表", m_lpstrId, out info))
            {
                // 如果 会员简表内没有这个记录
                MessageBox.Show("没有该会员!", "提示");
                return;
            }

            ////////////////// 提示充值者信息  //////////////
            string stringbuild = "\n 充值账号:" + m_lpstrId + "   |   充值金额:" + m_intMoney.ToString() + ".00 元\n充值者姓名:" + info.name;
            if (MessageBox.Show("确认充值吗？" + stringbuild, "人工充值", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                // 进入充值
                List<string> key = new List<string>();
                key.Add("流水单号");
                key.Add("充值金额");
                key.Add("会员编号");
                key.Add("会员账户");
                key.Add("会员姓名");
                key.Add("时间");
                List<string> value = new List<string>();
                value.Add("'000000001'");
                value.Add(m_intMoney.ToString());
                value.Add(info.id);
                value.Add("'" + info.user   + "'");
                value.Add("'" + info.name   + "'");
                value.Add("'" + DateTime.Now.ToLocalTime().ToString("yyyy-MM-dd HH:mm:ss") + "'");
                MySqlHelper.insert("充值记录表",key,value);
                //string [] key = new string[]
                return;
            }
                //System.Environment.Exit(0);


        }

        private void textBox充值账号_Leave(object sender, EventArgs e)
        {
            textBox充值账号_Enter(null, null);
        }

        private void textBox充值账号_Enter(object sender, EventArgs e)
        {
            
            // 如果账号为提示信息，则变为空
            if (textBox充值账号.Text.Equals(m_lpstrIdHit))
            {
                textBox充值账号.Text = "";
            }
            // 如果为空，则变为提示信息
            else if (textBox充值账号.Text.Equals(""))
            {
                textBox充值账号.Text = m_lpstrIdHit;
            }
        }

        private void textBox充值金额_Enter(object sender, EventArgs e)
        {
            if (textBox充值金额.Text.Equals(m_lpstrMoneyHit))
            {
                textBox充值金额.Text = "";
            }
            // 如果为空，则变为提示信息
            else if (textBox充值金额.Text.Equals(""))
            {
                textBox充值金额.Text = m_lpstrMoneyHit;
            }
        }

        private void textBox充值金额_Leave(object sender, EventArgs e)
        {
            textBox充值金额_Enter(null, null);
        }

        private void textBox充值账号_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsNumber(e.KeyChar) && e.KeyChar != (char)8)
            {
                e.Handled = true;
            }
        }

        private void textBox充值金额_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                button充值_Click(null, null);
            }
        }
    }
}
